package com.jinxinol.basic.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinxinol.basic.system.domain.SysPosition;
import com.jinxinol.basic.system.domain.SysRole;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.domain.SysUserPositioin;
import com.jinxinol.basic.system.domain.SysUserrole;
import com.jinxinol.basic.system.mapper.SysUserMapper;
import com.jinxinol.basic.system.mapper.SysUserPositioinMapper;
import com.jinxinol.basic.system.service.ISysRoleService;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.basic.system.service.ISysUserroleService;
import com.jinxinol.core.common.AbstractService;
import com.jinxinol.core.util.MD5;

/**
 * @author Automatic generation * 用户表Service
 */
@Service
public class SysUserService extends AbstractService<SysUser> implements ISysUserService{
	
	@Autowired
	protected void setMapper(SysUserMapper sysUserMapper) {
		this.mapper = sysUserMapper;
	}
	
	@Resource ISysRoleService sysRoleService;
	@Resource SysUserPositioinMapper userPositionMapper;
	@Resource ISysUserroleService userroleService;
	
	/**
	 *  查询所有用户
	 */
	@Override
	public List<SysUser> find(SysUser sysUser) throws Exception {
		return ((SysUserMapper)mapper).find(sysUser);
	}
	/**
	 * 用户登录
	 * */
	@Override
	public SysUser userLogin(String uname, String pword) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		MD5 d = new MD5();
		map.put("uname", uname);
		map.put("pword", d.getMD5ofStr(pword));
		return ((SysUserMapper) mapper).userLogin(map);
	}

	/**
	 * 查询用户关联信息
	 * */
	@Override
	public SysUser selectByUserMore(SysUser user) throws Exception {
		//System.out.println("查询用户权限");
		user.setUserRight(((SysUserMapper) mapper).selectUserRight(user));
		//System.out.println("查询用户角色");
		user.setRoleList(((SysUserMapper) mapper).selectUserRole(user));
		//System.out.println("查询用户职务");
		user.setPositionList(((SysUserMapper) mapper).selectUserPosition(user));
		return user;
	}

	@Override
	public List<SysPosition> selectUserPosition( SysUser user) throws Exception {
		return ((SysUserMapper) mapper).selectUserPosition(user);
	}

	@Override
	public void saveUserPosition(SysUser user) throws Exception {
		//删除原有副职信息
		SysUserPositioin sup = new SysUserPositioin();
		sup.setUser_id(user.getId());
		userPositionMapper.delete(sup);
		
		//重新创建副职信息
		String posIds = user.getPosIds();
		if(posIds == null || posIds.length() == 0){
			return;
		}
		String[] idArr = posIds.split(";");
		for(String id : idArr){
			SysUserPositioin temp = new SysUserPositioin();
			temp.setUser_id(user.getId());
			temp.setPosition_id(Integer.valueOf(id));
			temp.setType(2);	//副职
			userPositionMapper.insert(temp);
		}
		
		//更新主职
		SysUser tempUser = new SysUser();
		tempUser.setId(user.getId());
		tempUser.setPosition_id(user.getPosition_id());
		mapper.update(tempUser);
	}

	@Override
	public List<SysRole> selectUserRole(SysUser user) throws Exception {
		return ((SysUserMapper) mapper).selectUserRole(user);
	}

	@Override
	public List<SysRole> selectOtherRole(SysUser user) throws Exception {
		return ((SysUserMapper) mapper).selectOtherRole(user);
	}

	@Override
	public void saveUserRole(SysUser user) throws Exception {
		//删除用户原有的角色
		SysUserrole sur = new SysUserrole();
		sur.setUserid(user.getId());
		userroleService.delete(sur);
		
		//重新设置用户的角色
		String roleIds = user.getRoleIds();
		if(roleIds.split("_").length == 2){
			String rs = roleIds.split("_")[0];
			String ss = roleIds.split("_")[1];
			String[] rsArr = rs.split(",");
			String[] ssArr = ss.split(",");
			
			for(int i=0; i<rsArr.length; i++){
				SysUserrole temp = new SysUserrole();
				temp.setUserid(user.getId());
				temp.setRoleid(Integer.valueOf(rsArr[i]));
				temp.setSub_id(Integer.valueOf(ssArr[i]));
				
				userroleService.insert(temp);
			}
		}
	}
}
